package charts;

import java.awt.Color;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import org.jCharts.axisChart.AxisChart;
import org.jCharts.axisChart.ScatterPlotAxisChart;
import org.jCharts.chartData.ScatterPlotDataSeries;
import org.jCharts.chartData.ScatterPlotDataSet;
import org.jCharts.properties.AxisProperties;
import org.jCharts.properties.ChartProperties;
import org.jCharts.properties.DataAxisProperties;
import org.jCharts.properties.LegendProperties;
import org.jCharts.properties.LineChartProperties;
import org.jCharts.properties.PointChartProperties;
import org.jCharts.properties.PropertyException;
import org.jCharts.properties.ScatterPlotProperties;

public class XYChart {
	public final static Color[] mesCouleur = {
		Color.RED,
		Color.BLUE,
		Color.GREEN,
		Color.BLACK,
		Color.ORANGE, 
		Color.GRAY, 
		Color.CYAN,
		Color.DARK_GRAY,
		Color.LIGHT_GRAY,
		Color.PINK,
		Color.MAGENTA,
		Color.YELLOW
	};
	
	public static AxisChart getChart(String titreChart, String xTitre, String yTitre, String label, HashMap<Integer, Double> lesDonnee, int sizeLargeur, int sizeHauteur) throws PropertyException {
		
		Stroke[] strokes = new Stroke[]{LineChartProperties.DEFAULT_LINE_STROKE};
		//Shape[] shapes = new Shape[]{PointChartProperties.SHAPE_CIRCLE};
		Shape[] shapes = new Shape[]{null};
		Double min = null;
		Double max = null;
		Double somme = new Double(0);
		
		ScatterPlotDataSet scatterPlotDataSet = new ScatterPlotDataSet( new ScatterPlotProperties( strokes, shapes ) );

		Set<Integer> lesDates = lesDonnee.keySet();
		ArrayList<Integer> lDate = new ArrayList<Integer>(lesDates);
		Collections.sort(lDate);
		
		Point2D.Double[] points = new Point2D.Double[ lDate.size() ];
			
		if(lDate.size() > 0){
			for(int j=0;j<lDate.size();j++){
				points[j] = ScatterPlotDataSet.createPoint2DDouble();
				points[j].setLocation( j,lesDonnee.get(lDate.get(j)));
				if(min==null)min=lesDonnee.get(lDate.get(j));
				if(max==null)max=lesDonnee.get(lDate.get(j));
				if(min>lesDonnee.get(lDate.get(j)))min=lesDonnee.get(lDate.get(j));
				if(max<lesDonnee.get(lDate.get(j)))max=lesDonnee.get(lDate.get(j));
				somme+=lesDonnee.get(lDate.get(j));
			}
			scatterPlotDataSet.addDataPoints( points, Color.red, label );
		}else{
			points = new Point2D.Double[ 2 ];
			points[0] = ScatterPlotDataSet.createPoint2DDouble();
			points[0].setLocation(0,0);
			points[1] = ScatterPlotDataSet.createPoint2DDouble();
			points[1].setLocation(1,0);
			scatterPlotDataSet.addDataPoints( points, Color.red, "q");
		}
		
		DataAxisProperties xAxisProperties = new DataAxisProperties();
		
		if(min!=null && max!=null){
			xAxisProperties.setUserDefinedScale( 0, 1 );
			xAxisProperties.setNumItems( lesDates.size() );
		}else{
			xAxisProperties.setUserDefinedScale( -1, 1 );
			xAxisProperties.setNumItems( 10 );
		}
		xAxisProperties.setRoundToNearest( 0 );

		DataAxisProperties yAxisProperties = new DataAxisProperties();
		if(min!=null && max!=null){
			yAxisProperties.setUserDefinedScale( min*0.8, max*1.1 );
			yAxisProperties.setNumItems( 2 );
		}else{
			yAxisProperties.setUserDefinedScale( 0, 1 );
			yAxisProperties.setNumItems( 10 );
		}
		yAxisProperties.setRoundToNearest( 0 );

		AxisProperties axisProperties = new AxisProperties( xAxisProperties, yAxisProperties );
		//AxisProperties axisProperties = new AxisProperties( );
		ChartProperties chartProperties = new ChartProperties();
		LegendProperties legendProperties = new LegendProperties();
		
		ScatterPlotDataSeries scatterPlotDataSeries = new ScatterPlotDataSeries( scatterPlotDataSet,xTitre,yTitre,titreChart );
		AxisChart scatterPlotAxisChart = new ScatterPlotAxisChart( scatterPlotDataSeries,chartProperties,axisProperties,legendProperties,sizeLargeur,sizeHauteur );
		return scatterPlotAxisChart;
	}
	
	public static AxisChart getChart(String titreChart, String xTitre, String yTitre, String labelCourbe,ArrayList<Double[][]> lesDonnees, int sizeLargeur, int sizeHauteur) throws PropertyException {
		/*
		Double[][] exemple={{new Double(0),new Double(1),new Double(2),new Double(3),new Double(4)},{new Double(0),new Double(1),new Double(2),new Double(3),new Double(4)}};
		lesDonnees = new ArrayList<Double[][]>();
		lesDonnees.add(exemple);
		*/
		
		Stroke[] strokes;
		Shape[] shapes;
		if(lesDonnees.size() > 0){
			strokes = new Stroke[lesDonnees.size()];
			shapes = new Shape[lesDonnees.size()];
			for(int i=0;i<lesDonnees.size();i++){
				 strokes[i]=LineChartProperties.DEFAULT_LINE_STROKE;
				 shapes[i]=null;
			}
		}else{
			strokes = new Stroke[1];
			shapes = new Shape[1];
			strokes[0]=LineChartProperties.DEFAULT_LINE_STROKE;
			shapes[0]=null;
		}
		
		Double minX = null;
		Double maxX = null;
		
		Double minY = null;
		Double maxY = null;
		
		int maxI = 1;
		
		ScatterPlotDataSet scatterPlotDataSet = new ScatterPlotDataSet( new ScatterPlotProperties( strokes, shapes ) );
		
		boolean nonVide=true;
		for(int i=0;i<lesDonnees.size();i++){
			Double[][] lesDonnee = lesDonnees.get(i);
			String labelCourbeTmp = labelCourbe+" "+i;
			Point2D.Double[] points = new Point2D.Double[ lesDonnee[0].length ];
			for(int j=0;j<lesDonnee[0].length;j++){
				nonVide=false;
				points[j] = ScatterPlotDataSet.createPoint2DDouble();
				points[j].setLocation(lesDonnee[0][j],lesDonnee[1][j]);
				
				if(minX==null)minX=lesDonnee[0][j];
				if(maxX==null)maxX=lesDonnee[0][j];
				if(minX>lesDonnee[0][j])minX=lesDonnee[0][j];
				if(maxX<lesDonnee[0][j])maxX=lesDonnee[0][j];
				
				if(minY==null)minY=lesDonnee[1][j];
				if(maxY==null)maxY=lesDonnee[1][j];
				if(minY>lesDonnee[1][j])minY=lesDonnee[1][j];
				if(maxY<lesDonnee[1][j])maxY=lesDonnee[1][j];
				
				if(maxI<j+1)maxI=j+1;
			}
			if(nonVide){
				points = new Point2D.Double[ 2 ];
				points[0] = ScatterPlotDataSet.createPoint2DDouble();
				points[0].setLocation(0,0);
				points[1] = ScatterPlotDataSet.createPoint2DDouble();
				points[1].setLocation(1,0);
			}
			scatterPlotDataSet.addDataPoints( points, mesCouleur[i], labelCourbeTmp );
		}
		
		DataAxisProperties xAxisProperties = new DataAxisProperties();
		
		if(minX!=null && maxX!=null){
			System.out.println("minX: "+minX+" maxX: "+maxX);
			xAxisProperties.setUserDefinedScale( minX-1, (maxX-minX-1)/10 );
			
			xAxisProperties.setNumItems(10 );
		}else{
			xAxisProperties.setUserDefinedScale( 0, 1 );
			xAxisProperties.setNumItems( 10 );
		}
		xAxisProperties.setRoundToNearest( 0 );

		DataAxisProperties yAxisProperties = new DataAxisProperties();
		if(minY!=null && maxY!=null){
			System.out.println("minY: "+minY+" maxY: "+maxY+" maxI: "+maxI);
			yAxisProperties.setUserDefinedScale( minY-1, (maxY-minY-1)/10);
			
			yAxisProperties.setNumItems(10 );
		}else{
			yAxisProperties.setUserDefinedScale( 0, 1 );
			yAxisProperties.setNumItems( 10 );
		}
		yAxisProperties.setRoundToNearest( 0 );

		AxisProperties axisProperties = new AxisProperties( xAxisProperties, yAxisProperties );
		ChartProperties chartProperties = new ChartProperties();
		LegendProperties legendProperties = new LegendProperties();
		
		ScatterPlotDataSeries scatterPlotDataSeries = new ScatterPlotDataSeries( scatterPlotDataSet,xTitre,yTitre,titreChart );
		AxisChart scatterPlotAxisChart = new ScatterPlotAxisChart( scatterPlotDataSeries,chartProperties,axisProperties,legendProperties,sizeLargeur,sizeHauteur );
		return scatterPlotAxisChart;
	}
}
